package com.dq.Controller;
import com.dq.Pojo.Result;
import com.dq.Pojo.Student;
import com.dq.Service.StuService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.Intercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpSession;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/stu")
public class StuController {
    @Autowired
    private StuService stuService;
//添加学生信息
    @PostMapping("/addStu")
    public Result<Student> addStu(@RequestBody Student student){

        stuService.addstu(student);
        return Result.success(student);
    }
    //查找信息
    @GetMapping("/findstu")
    public Result<Student> findstu(@RequestParam Integer id){
        Student student=stuService.findstu(id);
        if (student == null){
            return Result.error("失败");
        }
        return Result.success(student);
    }

    //信息修改
    @PutMapping("/updateStu")
    public Result<Student> updateStu(@RequestBody Student student){
        stuService.update(student);
        return Result.success(student);
    }
    //模糊分页查询
    @GetMapping("/findAll")
    public Result<PageInfo<Student>> stuSearch(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize,
                                      @RequestParam String name,
                                      @RequestParam String stuno){
        PageInfo<Student> pagelist =stuService.findAll(pageNum,pageSize,name,stuno);
        return Result.success(pagelist);
    }
    //删除
    @DeleteMapping("/studel")
    public Result del(@RequestParam Integer id){
        stuService.delete(id);
        return Result.success();
    }

    @GetMapping("/findByclassid")
    public Result find(@RequestParam Integer classId){
        List<Student> studentList=stuService.findByclassid(classId);
        return Result.success(studentList);
    }

}
